<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>基于阿里云手机验证码 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>基于阿里云手机验证码</h1>
  <time datetime=2020-05-12T10:14:30&#43;0800 class="post-date">Tue, May 12, 2020</time>
  <p>下载</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue"><span style="color:#a6e22e">pip</span> <span style="color:#a6e22e">install</span> <span style="color:#a6e22e">aliyun</span><span style="color:#f92672">-</span><span style="color:#a6e22e">python</span><span style="color:#f92672">-</span><span style="color:#a6e22e">sdk</span><span style="color:#f92672">-</span><span style="color:#a6e22e">core</span>
</code></pre></div><p>导包</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 阿里云短信</span>
<span style="color:#f92672">from</span> aliyunsdkcore.client <span style="color:#f92672">import</span> AcsClient
<span style="color:#f92672">from</span> aliyunsdkcore.request <span style="color:#f92672">import</span> CommonRequest
</code></pre></div><p>redis连接</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#定义ip地址和端口</span>
host <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;120.26.175.79&#34;</span>
port <span style="color:#f92672">=</span> <span style="color:#ae81ff">6379</span>

<span style="color:#75715e">#链接对象</span>
r <span style="color:#f92672">=</span> redis<span style="color:#f92672">.</span>Redis(host<span style="color:#f92672">=</span>host, port<span style="color:#f92672">=</span>port, decode_responses<span style="color:#f92672">=</span>True)
</code></pre></div><p>生成验证码函数</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get_random_str</span>():
    <span style="color:#75715e">#验证码是由 字母a~z  A~Z 数字 0~9 组成</span>
    <span style="color:#75715e"># 在 ascii 码中 小写a的起点是97 大写A起点是65</span>
    a_ <span style="color:#f92672">=</span> [chr(var) <span style="color:#66d9ef">for</span> var <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">97</span>, <span style="color:#ae81ff">123</span>)]  <span style="color:#75715e"># 小写a的列表推导式</span>
    A_ <span style="color:#f92672">=</span> [chr(var) <span style="color:#66d9ef">for</span> var <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">65</span>, <span style="color:#ae81ff">91</span>)]   <span style="color:#75715e"># 大写A的列表推导式</span>
    num_ <span style="color:#f92672">=</span> [str(var) <span style="color:#66d9ef">for</span> var <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">9</span>)]  <span style="color:#75715e"># 数字的</span>
    <span style="color:#75715e"># 使用sample 在列表中随机生成6个任意字母数字</span>
    <span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#39;&#39;</span><span style="color:#f92672">.</span>join(random<span style="color:#f92672">.</span>sample(a_ <span style="color:#f92672">+</span> A_ <span style="color:#f92672">+</span> num_, <span style="color:#ae81ff">6</span>))
</code></pre></div><p>阿里云发送验证码</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">SendAlysms</span>(APIView):
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">post</span>(self,request):
        phone <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>data<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;phone&#39;</span>)  <span style="color:#75715e"># 接收到用户发来的验证码</span>
        res <span style="color:#f92672">=</span> {}
        token <span style="color:#f92672">=</span> r<span style="color:#f92672">.</span>get(phone)
        <span style="color:#75715e"># token = &#39;123456&#39;</span>
</code></pre></div><p>​</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">        <span style="color:#66d9ef">if</span> token:  <span style="color:#75715e"># 去redis 中查看 是否这个手机号</span>
            res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">401</span>
            res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;请一分钟后再次发送&#34;</span>
            <span style="color:#66d9ef">return</span> JsonResponse(res)
</code></pre></div><p>​</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">        <span style="color:#66d9ef">else</span>:
            token <span style="color:#f92672">=</span> get_random_str()  <span style="color:#75715e"># 生成验证码</span>
            AK <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;LTAI4G7emXUN3dnGVK2p1duM&#34;</span>
            SK <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;ZgekQkkT1cZZ4hCyhSquxOwwnjyfLo&#34;</span>

            <span style="color:#75715e"># 登录阿里云客户端</span>
            client <span style="color:#f92672">=</span> AcsClient(AK, SK, <span style="color:#e6db74">&#39;cn-hangzhou&#39;</span>)

            request <span style="color:#f92672">=</span> CommonRequest()
            request<span style="color:#f92672">.</span>set_accept_format(<span style="color:#e6db74">&#39;json&#39;</span>)
            request<span style="color:#f92672">.</span>set_domain(<span style="color:#e6db74">&#39;dysmsapi.aliyuncs.com&#39;</span>)
            request<span style="color:#f92672">.</span>set_method(<span style="color:#e6db74">&#39;POST&#39;</span>)
            request<span style="color:#f92672">.</span>set_protocol_type(<span style="color:#e6db74">&#39;https&#39;</span>)  <span style="color:#75715e"># https | http</span>
            request<span style="color:#f92672">.</span>set_version(<span style="color:#e6db74">&#39;2017-05-25&#39;</span>)
            request<span style="color:#f92672">.</span>set_action_name(<span style="color:#e6db74">&#39;SendSms&#39;</span>)

            request<span style="color:#f92672">.</span>add_query_param(<span style="color:#e6db74">&#39;RegionId&#39;</span>, <span style="color:#e6db74">&#34;cn-hangzhou&#34;</span>)
            <span style="color:#75715e"># 这个手机号可以从前端获取</span>
            request<span style="color:#f92672">.</span>add_query_param(<span style="color:#e6db74">&#39;PhoneNumbers&#39;</span>, str(phone))
            request<span style="color:#f92672">.</span>add_query_param(<span style="color:#e6db74">&#39;SignName&#39;</span>, <span style="color:#e6db74">&#34;美多商场&#34;</span>)
            request<span style="color:#f92672">.</span>add_query_param(<span style="color:#e6db74">&#39;TemplateCode&#39;</span>, <span style="color:#e6db74">&#34;SMS_189015076&#34;</span>)
            <span style="color:#75715e"># 验证码可以随机获取</span>
            request<span style="color:#f92672">.</span>add_query_param(<span style="color:#e6db74">&#39;TemplateParam&#39;</span>, str({<span style="color:#e6db74">&#39;code&#39;</span>:token}))
            r<span style="color:#f92672">.</span>set(phone, token)  <span style="color:#75715e"># 存储到redis</span>
            <span style="color:#75715e"># 设置访问周期为60秒</span>
            r<span style="color:#f92672">.</span>expire(phone, <span style="color:#ae81ff">60</span>)

            response <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>do_action(request)
            <span style="color:#66d9ef">print</span>(str(response, encoding<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;utf-8&#39;</span>))
            res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">200</span>
            res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;验证码已发送&#34;</span>
            <span style="color:#75715e"># res[&#39;token&#39;] = phone</span>
            <span style="color:#66d9ef">return</span> JsonResponse(res)
</code></pre></div><p>校验验证码以及存储数据库</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 如果传入的是电话</span>
            <span style="color:#66d9ef">if</span> phone:
                token <span style="color:#f92672">=</span> r<span style="color:#f92672">.</span>get(phone)
                <span style="color:#75715e"># 在数据库中取出对应的验证码</span>
                <span style="color:#66d9ef">if</span> token <span style="color:#f92672">and</span> token <span style="color:#f92672">==</span> code:
                    res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">200</span>
                    res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;注册成功&#34;</span>
                    User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>create_user(
                        username<span style="color:#f92672">=</span>username, password<span style="color:#f92672">=</span>password, phone<span style="color:#f92672">=</span>phone)
                    <span style="color:#66d9ef">return</span> Response(res)

                <span style="color:#66d9ef">else</span>:
                    res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">403</span>
                    res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;验证码错误&#34;</span>
                    <span style="color:#66d9ef">return</span> Response(res)
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
